package com.ljph.ljaker.interceptor;

import com.jfinal.aop.Interceptor;
import com.jfinal.aop.Invocation;
import com.ljph.jfinal.base.BaseController;
import com.ljph.ljaker.app.AppConfig;
import com.ljph.ljaker.model.User;

import javax.servlet.http.HttpServletResponse;

/**
 * Created by yuzhou on 16/9/24.
 */
public class LoginInterceptor implements Interceptor {

    @Override
    public void intercept(Invocation inv) {

        BaseController controller = (BaseController)inv.getController();
        User user = controller.getSessionAttr(AppConfig.SESSION_USER);

        if(user == null) {
            HttpServletResponse response = controller.getResponse();
            response.setHeader("login", "unLogin");
            response.setStatus(401);
            controller.renderHtml("未登录用户禁止访问");
        } else {
            inv.invoke();
        }
    }
}
